// pages/market/comment/comment.js
import api from '../../../utils/api'
import message from '../../../utils/message'
Page({

    /**
     * 页面的初始数据
     */
    data: {

        product_id:null,
        page_index:1,
        has_load:false,
        comment_list:[],
        comment_summary:null,
        no_data:false,
        status:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.getShopCommentIndex();

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getShopCommentIndex:function () {
        var me = this;
        message.show_loading();
        api.shop.getShopCommentIndex( {} , function ( response ) {
            message.hide_loading();

            if( response.status==1 ){


                var arr = response.data.comment_list;
                
                var len = arr.length;
                for(var i=0;i<arr.length;i++){

                    var t = JSON.parse( arr[i].images )
                    if(t){

                        arr[i].images = t;
                    }else{
                        arr[t] = []
                    }
                }

                me.setData({
                    comment_list: arr,
                    has_load:true,
                    comment_summary: response.data.comment_summary
                })

            }else{
                message.show_error( response );
            }
        })
        
    },
    getShopCommentList: function(){
        var me = this;
        var params = {}
        var page_index = this.data.page_index;
        if(this.data.has_load){
            page_index++;
        }
        params.page_index = page_index
        params.type = this.data.status;  //好评 中评 差评


        message.show_loading();
        api.shop.getShopCommentList( params , function (response) {
            message.hide_loading();
            if(response.status==1){

                var arr = response.data;
                var len = arr.length;

                for( var i=0;i<len ; i++ ){

                    var t = JSON.parse( arr[i].images )
                    if(t){

                        arr[i].images = t;
                    }else{
                        arr[t] = []
                    }

                }

                var no_data = false;

                if(len<10){
                    no_data = true;
                }

                
                var comment_list = me.data.comment_list;
                
                comment_list = comment_list.concat(arr)


                me.setData({
                    no_data:no_data,
                    page_index: page_index,
                    comment_list: comment_list,
                    has_load: true
                })

            }
        })
    },
    showCommentImage:function(e){
        var obj = e.currentTarget.dataset;
        var index = obj.index;
        var url = obj.m;
        var image_list = this.data.comment_list[index].images;
        wx.previewImage({
            current:url,
            urls: image_list
        })

    },
    selectStatus:function (e) {
        var status = e.currentTarget.dataset.status
        this.setData({
            status: status,
            has_load: false,
            comment_list:[] 
        })

        this.getShopCommentList();
    }
})